package com.y.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.y.config.exception.AccountLoginException;
import com.y.entity.User;
import com.y.entity.vo.*;
import com.y.service.UserService;
import com.y.utils.ConstantName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yjh
 * @since 2021-09-24
 */
@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    UserService userService;



    @GetMapping("/registerView")
    public String registerView(){
        return "view/system/operate/user/add";
    }

    /**
     * @param username 检查用户名
     * @return
     */
    @RequestMapping("/checkUserName")
    @ResponseBody
    public Boolean checkUserName(@RequestParam("username") String username){
        return userService.getOne(new QueryWrapper<User>().eq("username",username)) == null;
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    public R registerUser(RegisterUser user){
        return userService.registerUser(user);
    }

    @PostMapping("/updatePassword")
    @ResponseBody
    public R updatingPassword(UpdateUserPassword password){
        return userService.updatePassword(password);
    }

    @GetMapping("/updatePassword")
    public String updatePassword(){
        return "view/system/operate/user/updatePassword";
    }



    /**
     * 分页
     * @param page 页码
     * @param limit 页面数量
     * @param username 模糊查询用户名
     * @return DataTable 数据表格
     */
    @ResponseBody
    @RequestMapping("/list")
    public DataTable<User> pageList(@RequestParam(value = "page",defaultValue = "1") int page,
                                           @RequestParam(value = "limit",defaultValue = "10") int limit,
                                           @RequestParam(value = "username",required = false) String username){

        if (username == null)
            return userService.getListUser(page, limit);

        return userService.getListUserByUsername(page, limit, username);
    }


    @GetMapping("/getOneUser/{id}")
    public String getOneUser(@PathVariable int id, Model model){
        User user = userService.getById(id);
        model.addAttribute("user",user);
        return "view/system/operate/user/edit";
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    @ResponseBody
    public R updateUser(User user){
        R r = R.success();
        return userService.updateById(user) ? r.setMessage("操作成功") : r.setMessage("操作失败");
    }

    /**
     * 删除用户
     * @param id 用户ID
     * @return
     */
    @DeleteMapping("/removeUser/{id}")
    @ResponseBody
    public R removeUser(@PathVariable int id){
        R r = R.success();
        return userService.removeById(id) ? r.setMessage("删除成功") : r.setMessage("操作失败");
    }

}
